package com.shou.infrastructure.persistent.repository.user.mapstruct;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shou.domain.user.model.entity.UserEntity;
import com.shou.infrastructure.persistent.po.UserPO;
import org.mapstruct.Mapper;// 注意导包不要出错

import java.util.List;
import java.util.stream.Collectors;

// 可以安装插件MapStruct Support
// 也可以不使用MapStruct，而使用set方法
@Mapper(componentModel = "spring")
public interface UserPOConvert {

    UserPO convertUserEntityToPO(UserEntity userEntity);
    UserEntity convertUserPOToEntity(UserPO userPO);

    // 批量转换，会依据单体转换的规则进行转换
    List<UserPO> convertUserEntityToPOList(List<UserEntity> userEntityList);
    List<UserEntity> convertUserPOToUserList(List<UserPO> userPOList);

    default IPage<UserEntity> convertUserPOPageToEntity(IPage<UserPO> pageMessage){
        if (pageMessage == null || pageMessage.getRecords() == null || pageMessage.getRecords().isEmpty()) {
            return null;
        }
        List<UserEntity> pageMessageEntities = pageMessage.getRecords().stream()
                .map(this::convertUserPOToEntity)
                .collect(Collectors.toList());

        IPage<UserEntity> entityPage = new Page<>();
        entityPage.setRecords(pageMessageEntities);
        entityPage.setTotal(pageMessage.getTotal());
        entityPage.setSize(pageMessage.getSize());
        entityPage.setCurrent(pageMessage.getCurrent());
        return entityPage;
    }
}
